<?php


namespace app\controller\api;


use app\controller\api\Base;
use app\model\Attachment;
use app\model\ConfigModel;
use app\model\DeliveryStepModel;
use app\model\DriverModel;
use app\model\GoodsModel;
use app\model\QuotationModel;
use app\model\QuotationTransportModel;
use app\model\ReleaseScopeModel;
use app\model\RemarkModel;
use app\model\SizeModel;
use app\model\VehicleModel;
use app\Request;
use app\validate\BasicCheck;
use think\App;
use think\db\exception\DbException;
use think\facade\Db;
use app\common\library\Translation;
use app\model\DeliveryModel;
use app\model\AddressModel;

class Basic extends Base
{
    public $token;
    public $version;
    public $lang;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->token = $this->request->header('xzdToken');
        $this->version = $this->request->header('version');
        $this->lang = $this->request->header('lang');
    }


    /**
     * @param Request $request
     * @param BasicCheck $check
     * @param DriverModel $driverModel
     * @notes 员工列表
     * @author: dongdong
     * @date: 2025/1/18 10:30
     */
    public function staff_lists(Request $request, BasicCheck $check, DriverModel $driverModel)
    {
        try {
            $param = $request->param();
            $param['token'] = $this->token;
            $param['version'] = $this->version;
            $param['lang'] = $this->lang;


            $result = $check->scene('staff_lists')->check($param);
            if (!$result) {
                $this->api_result(400, $check->getError(), []);
            } else {
                $data = $driverModel->staff_lists($param);
                $this->api_result(200, '请求成功', ['data' => $data]);
            }
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败!', _d($e));
        }
    }

    /**
     * @param ConfigModel $configModel
     * @notes 员工类型选择
     * @author: dongdong
     * @date: 2025/1/13 12:09
     */
    public function staff_type_select(ConfigModel $configModel)
    {
        $data = $configModel->select_type(7);
        $this->api_result(200, '请求成功', ['data' => $data]);
    }

    /**
     * @param Request $request
     * @param DriverModel $driverModel
     * @notes 修改员工状态
     * @author: dongdong
     * @date: 2025/1/18 11:45
     */
    public function staff_state(Request $request, DriverModel $driverModel)
    {
        $param = $request->param();
        $data = $driverModel->staff_state($param);
        if ($data) {
            $this->api_result(200, '请求成功', ['data' => $data]);
        } else {
            $this->api_result(400, '请求失败', ['data' => $data]);
        }
    }


    /**
     * @param Request $request
     * @param DriverModel $driverModel
     * @notes 修改员工状态
     * @author: dongdong
     * @date: 2025/1/18 11:45
     */
    public function staff_add(Request $request, DriverModel $driverModel)
    {
        $param = $request->param();
        $data = $driverModel->staff_add($param);
        if ($data == 1) {
            $this->api_result(200, '请求成功', ['data' => $data]);
        } else if ($data == 0) {
            $this->api_result(400, '请求失败', ['data' => $data]);
        } else {
            $this->api_result(400, $data, ['data' => $data]);
        }
    }

    /**
     * @throws \think\db\exception\DbException
     * @notes 员工删除
     * @author: dongdong
     * @date: 2025/1/20 15:53
     */
    public function staff_del()
    {
        $id = input("id") ?? 0;
        $w['id'] = $id;
        $wh = "state in (2,3)";
        Db::table("store_driver")->where($w)->where($wh)->update(['is_deleted' => 1]);
        $this->api_result(200, '请求成功', ['data' => []]);
    }

    /**
     * @param Request $request
     * @notes 我的同事
     * @author: dongdong
     * @date: 2025/2/20 11:36
     */
    public function my_colleague(Request $request)
    {
        $param = $request->param();
        $bind_user_id = $param['bind_user_id'];
        $Attachment = new Attachment();
        $data = Db::query("select * from store_driver where bind_user_id=$bind_user_id ");
        foreach ($data as $k => &$v) {
            $v['attachment'] = $Attachment->field("id,path")->where("id", "in", $v['attachment_id'])->find();
            $v['bind_user'] = Db::connect("fast_mysql")->table("fa_user")->field("id,username,username_ru")->where(['id' => $v['bind_user_id']])->find();
            $v['type_info'] = (new ConfigModel())->info($v['type']);
        }
        $this->api_result(200, '请求成功', ['data' => $data]);
    }


    /**
     * @param Request $request
     * @notes 身份变更
     * @author: dongdong
     * @date: 2025/2/20 11:36
     */
    public function identity_change(Request $request)
    {
        $param = $request->param();
        $user_id = $param['user_id'];
        $country = $param['country'];
        $district = $param['district'];
        $bind_user_id = $param['bind_user_id'];
        $name = $param['name'] ?? '';
        if ($name) {
            $Translation = new Translation();
            $name_arr = $Translation->translationSelf($name);
            $username = $name_arr['zh'];
            $username_ru = $name_arr['ru'];
            $nickname = $name_arr['zh'];
            Db::connect("fast_mysql")->table("fa_user")->where(['id' => $user_id])->update(['username' => $username, 'username_ru' => $username_ru, 'nickname' => $nickname]);
        }

        $avatar = $param['avatar'] ?? '';
        if ($avatar) {
            Db::connect("fast_mysql")->table("fa_user")->where(['id' => $user_id])->update(['avatar' => $avatar]);
        }


        $identity_id = Db::connect("fast_mysql")->table("fa_identity")->where(['country' => $country, 'type' => 6, 'district' => $district])->value('id');
        Db::connect("fast_mysql")->table("fa_user_info")->where(['user_id' => $user_id])->update(['identity_id' => $identity_id]);
        
        Db::table('store_driver')->where(['bind_user_id'=>$bind_user_id,'register_id'=>$user_id])->delete();
        (new DriverModel())->insert_boss($user_id);
        $this->api_result(200, '请求成功', ['data' => []]);
    }


    /**
     * @param Request $request
     * @throws DbException
     * @notes 身份设置
     * @author: dongdong
     * @date: 2025/2/20 14:16
     */
    public function identity_set(Request $request)
    {
        $param = $request->param();
        $user_id = $param['user_id'];
        $identity_id = $param['identity_id'];

        $country = $param['country'] ?? 0;
        $area_new_id = $param['area_new_id'] ?? 0;

        $name = $param['name'] ?? '';
        if ($name) {
            $Translation = new Translation();
            $name_arr = $Translation->translationSelf($name);
            $username = $name_arr['zh'];
            $username_ru = $name_arr['ru'];
            $nickname = $name_arr['zh'];
            Db::connect("fast_mysql")->table("fa_user")->where(['id' => $user_id])->update(['username' => $username, 'username_ru' => $username_ru, 'nickname' => $nickname]);
        }

        $avatar = $param['avatar'] ?? '';
        if ($avatar) {
            Db::connect("fast_mysql")->table("fa_user")->where(['id' => $user_id])->update(['avatar' => $avatar]);
        }
        $find = Db::connect("fast_mysql")->table("fa_user_info")->where(['user_id' => $user_id])->field("user_id")->find();
        if (empty($find)) {
            Db::connect("fast_mysql")->table("fa_user_info")->insert(['user_id' => $user_id]);
        }
        Db::connect("fast_mysql")->table("fa_user_info")->where(['user_id' => $user_id])->update(['identity_id' => $identity_id, 'country' => $country, 'area_new_id' => $area_new_id]);
        $this->api_result(200, '请求成功', ['data' => []]);
    }


    /**
     * @param Request $request
     * @param DeliveryModel $deliveryModel
     * @notes 我的任务
     * @author: dongdong
     * @date: 2025/2/20 13:58
     */
    public function my_task(Request $request, DeliveryModel $deliveryModel)
    {
        $param = $request->param();
        $user_id = $param['user_id'];
        $list = Db::query("select * from store_delivery where is_deleted=0 and find_in_set($user_id,task_user_id)");
        $list = $deliveryModel->record_handle($list, $user_id);
        $this->api_result(200, '请求成功', ['data' => $list]);
    }


    //码表分类
    public function cate(Request $request)
    {
        $param = $request->param();

        $type = $param['type'];
        $w = "";
        if ($type) {
            $w = "type='$type' and status='normal'";
        }
        $list = Db::connect("fast_mysql")->table("fa_code_table")->field("id,name,name_ru")->where($w)->select();
        $this->api_result(200, '请求成功', ['data' => $list]);
    }


    /**
     * @param VehicleModel $VehicleModel
     * @param DriverModel $driverModel
     * @param DeliveryStepModel $deliveryStepModel
     * @notes 我的车辆
     * @author: dongdong
     * @date: 2025/1/20 9:56
     */
    public function transport_index(VehicleModel $VehicleModel, DriverModel $driverModel, DeliveryStepModel $deliveryStepModel)
    {
        try {
            $status = input('status') ?? 0;

            $limit = input('size') ?? 10;


            $page = input('page') ?? 1;
            $page = ($page - 1) * 10;
            $type = input('type') ?? '';
            $types = input('types') ?? '';
            $mode = input('mode') ?? '0';
            $car_category_id = input('car_category_id') ?? '';
            if ($type) {
                $type_arr = explode(',', $type);
                $mode = $type_arr[0];
                $car_category_id = $type_arr[1];
            }


            $where['a.is_deleted'] = 0;
            if (!empty($status)) {
                if ($status == 1) {
                    $where['a.status'] = $status;
                } elseif ($status == 2) {
                    $where['a.status'] = $status;
                } elseif ($status == 3) {
                    $where['a.status'] = $status;
                } elseif ($status == 4) {
                    $where['a.status'] = $status;
                }
            }


            if ($mode) {
                $where['a.mode'] = $mode;
            }
            if ($car_category_id) {
                $where['a.car_category_id'] = $car_category_id;
            }

            $user_id = input('user_id') ?? '';


            $u_id = input('u_id') ?? '';


            $third = input('third') ?? '';
            if ($third) {
                $ids = '';
                if ($user_id) {
                    $register_select = Db::table("store_driver")->field("id,register_id")->where(['bind_user_id' => $user_id, 'type' => 32])
                        ->where("register_id!=$user_id")->select();
                    if ($register_select) {
                        foreach ($register_select as $k => &$v) {
                            if ($v['register_id'] != $user_id) {
                                $ids .= $v['id'] . ',';
                            }
                        }
                        $ids = trim($ids, ',');
                    }

                    $list = Db::table("store_car")->where($where)->where("driver_id", "in", $ids)->order("id desc")->limit($page, $limit)->select();


                } else {
                    $list = [];
                }
            } else {
               if ($types == 1) {
                    $where['a.user_id'] = $user_id;
                }else{
                   $where['b.register_id'] = $user_id;
               }

                if ($u_id) {
                    $where['user_id'] = $u_id;
                }

                $list = Db::table("store_car")
                    ->alias('a')
                    ->join('store_driver b','b.id = a.driver_id','left')
                    ->field('a.*')
                    ->where($where)->order("a.id desc")->limit($page, $limit)->select();
            }
            $arr = [];
            foreach ($list as $k => &$v) {
                $v['create_at'] = date('Y-m-d H:i:s', $v['createtime']);
                $v['category'] = $VehicleModel->field("title,title_ru")->where(["id" => $v['car_category_id']])->find();
                $v['current_city'] = $this->city($v['current_city_id']);

                if ($v['driver_id']) {
                    $v['driver'] = $driverModel->info($v['driver_id'], 1);
                } else {
                    $v['driver'] = $driverModel->info($v['user_id'], 0);
                }

                if (empty($v['driver_id']) && $v['mode'] ==2) {
                    $v['status'] = 4;
                }

                $v['step'] = $deliveryStepModel->last_step($v['id']);

                $transport_xian = '';
                $v['posit'] = '';

                $v['infoList'] = [
                    [
                        'title' => '载重', 'num' => $v['product_weight'] . 't'
                    ],
                    [
                        'title' => '线路', 'num' => $transport_xian
                    ],
                    [
                        'title' => '体积', 'num' => $v['product_volume'] . 'm³',
                    ],
                ];

                if ($v['order_id']) {
                    $v['transport_list'] = '';
                    $delivery = Db::table("store_delivery")->where(["id" => $v['order_id']])->find();
                    if ($delivery) {
                        $departure_id = $delivery['departure_id'];
                        $departure_area_id = Db::table("store_address")->where(["id" => $departure_id])->value('area_id');
                        $reach_id = $delivery['reach_id'];
                        $reach_area_id = Db::table("store_address")->where(["id" => $reach_id])->value('area_id');
                        $v['transport_list'] = $departure_area_id . ',' . $reach_area_id . ',0';
                    }
                }

                if ($v['transport_list']) {
                    $v['transport_lists'] = $this->transport_list($v['transport_list']);

                    if ($v['transport_lists']) {
                        $transport_find = $v['transport_lists'][0];
                        if ($transport_find) {
                            if ($transport_find['departure_city']) {
                                $departure_city_name = $transport_find['departure_city']['name'];
                            } else {
                                $departure_city_name = '';
                            }

                            if ($transport_find['reach_city']) {
                                $reach_city_name = $transport_find['reach_city']['name'];
                            } else {
                                $reach_city_name = '';
                            }

                            $transport_xian = $departure_city_name . ' ' . $reach_city_name;
                        } else {
                            $transport_xian = '';
                        }
                    }
                    $v['infoList'] = [
                        [
                            'title' => '体积', 'num' => $v['product_volume'] . 'm³',
                        ],
                        [
                            'title' => '载重', 'num' => $v['product_weight'] . 't'
                        ],
                        [
                            'title' => '线路', 'num' => $transport_xian
                        ]
                    ];
                } else {
                    $v['transport_lists'] = [];
                }

                if (($v['current_city']) && (count($v['current_city'])) >= 1) {
                    $v['posit'] = $v['current_city']['name'];
                }
                $v['luxian'] = [];

                if ($v['cfd_transports'] && $v['mdd_transports']) {
                    $cfd_transports = explode(';', $v['cfd_transports']);
                    $mdd_transports = explode(';', $v['mdd_transports']);
                    $arrs = [];
                    foreach ($cfd_transports as $kk => $vv) {
                        if ($vv) {
                            $ar = explode(',', $vv);
                            if ($ar[2] == '-1' && $ar[1] == '-1') {
                                $cfd = areas()[$ar[0]];
                            } elseif ($ar[2] == '-1' && $ar[1] != '-1') {
                                $cfd = $this->citys($ar[1]);
                            } else {
                                $cfd = $this->citys($ar[2]);
                            }
                            $cfd['id'] = $vv;
                            $arrs[$kk]['cfd'] = $cfd;
                        }
                    }
                    foreach ($mdd_transports as $kk => $vv) {
                        if ($vv) {
                            $ar = explode(',', $vv);
                            if ($ar[2] == '-1' && $ar[1] == '-1') {
                                $mdd = areas()[$ar[0]];
                            } elseif ($ar[2] == '-1' && $ar[1] != '-1') {
                                $mdd = $this->citys($ar[1]);
                            } else {
                                $mdd = $this->citys($ar[2]);
                            }
                            $mdd['id'] = $vv;
                            $arrs[$kk]['mdd'] = $mdd;
                        }
                    }
                    $v['luxian'] = $arrs;
                }
                array_push($arr, $v);
            }

            $count = Db::table("store_car")
                ->alias('a')
                ->join('store_driver b','b.id = a.driver_id','left')
                ->where($where)->count();
            $this->api_result(200, '请求成功', ['data' => ['list' => $arr, 'count' => $count]]);
        } catch (\Exception $e) {
            _d($e);
            $this->api_result(400, '请求失败', ['data' => ['list' => [], 'count' => 0]]);
        }
    }


    /**
     * @param $transport_list
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 运输路线
     * @author: dongdong
     * @date: 2025/1/20 14:04
     */
    public function transport_list($transport_list)
    {
        $tt = [];
        if ($transport_list) {
            $result = explode(';', $transport_list);
            foreach ($result as $k6 => &$v6) {
                $dd = explode(',', $v6);
                $departure_city = '';
                $reach_city = '';
                $speed = '';
                $price = '';
                if (!empty($dd[0])) {
                    $departure_city = $dd[0];
                }
                if (!empty($dd[1])) {
                    $reach_city = $dd[1];
                }
                if (!empty($dd[2])) {
                    $speed = $dd[2];
                }
                if (!empty($dd[3])) {
                    $price = $dd[3];
                }

                array_push($tt, ['departure_city' => $this->city($departure_city), 'reach_city' => $this->city($reach_city), 'speed' => $speed, 'price' => $price]);
            }
        }
        return $tt;
    }


    /**
     * @param $id
     * @return array|mixed|\think\db\BaseQuery|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 城市
     * @author: dongdong
     * @date: 2025/1/20 10:05
     */
    public function city($id)
    {
        if ($id > 0) {
            return Db::connect("fast_mysql")->table("fa_area")->field("id,shortname,shortname_ru,name,name_ru,mergename,mergename_ru")->where("id=$id")->find();
        } else {
            return Db::connect("fast_mysql")->table("fa_area")->field("id,shortname,shortname_ru,name,name_ru,mergename,mergename_ru")->where("id=5055")->find();;
        }
    }

    /**
     * @param $id
     * @return array|mixed|\think\db\BaseQuery|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 城市
     * @author: dongdong
     * @date: 2025/1/20 10:03
     */
    public function city2($id)
    {
        return Db::connect("fast_mysql")->table("fa_release_area")->field("id,country,province,city,district,country_name,country_name_ru,province_name,province_name_ru,city_name,city_name_ru,district_name,district_name_ru,detail,mergename,mergename_ru")
            ->where("id=$id")->find();
    }


    /**
     * @param DriverModel $driverModel
     * @return DriverModel[]|array|\think\Collection
     * @notes 司机查询
     * @author: dongdong
     * @date: 2025/1/20 15:00
     */
    public function driver_select(DriverModel $driverModel)
    {
        try {
            $user_id = input("user_id");
            $data = $driverModel->where("(bind_user_id=$user_id and type=32) or register_id=$user_id")->select();
            $this->api_result(200, '请求成功', ['data' => $data]);
        } catch (\Exception $e) {
            return [];
        }
    }


    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 运输库里面管理 当前城市位置
     * @author: dongdong
     * @date: 2025/1/20 15:25
     */
    public function current_city_id()
    {
        $id = input("id") ?? '';
        $user_id = input("user_id") ?? '';
        $current_city_id = input("current_city_id");
        $find = Db::table("store_car")->where("id=$id")->find();
        if ($find) {
            if ($find['current_city_id'] != $current_city_id) {
                Db::table("store_car")->where("id=$id")->update(['current_city_id' => $current_city_id]);
                $fa_area = Db::connect("fast_mysql")->table("fa_area")->where(['id' => $current_city_id])->find();
                if ($fa_area) {
                    Db::table("store_car")->where("id=$id")->update(['current_province_id' => $fa_area['pid'], 'current_country' => $fa_area['country']]);
                }
            }
            $this->api_result(200, '请求成功', ['data' => []]);
        } else {
            $this->api_result(400, '这不是您的车哦', ['data' => []]);
        }
    }


    /**
     * @notes  运输库 修改车辆状态
     * @author: dongdong
     * @date: 2025/1/23 8:56
     */
    public function current_car_status()
    {
        $id = input("id") ?? '';
        $user_id = input("user_id") ?? '';
        $find = Db::table("store_car")->where("id=$id")->find();
        if ($find) {
            $status = input("status");
            try {
                Db::table("store_car")->where("id=$id")->update(['status' => $status]);
                $this->api_result(200, '请求成功', ['data' => []]);
            } catch (DbException $e) {
                $this->api_result(400, '请求失败', ['data' => []]);
            }
        } else {
            $this->api_result(400, '这不是您的车哦', ['data' => []]);
        }
    }


    /**
     * @notes 运输车辆 更新与添加
     * @author: dongdong
     * @date: 2025/1/20 11:42
     */
    public function transport_save()
    {
        try {
            $Translation = new Translation();
            $name = input("name") ?? '';
            $name_ru = $name;
            if ($name) {
                $wenzi = $Translation->languageDetect($name);
                $language = $wenzi['Lang'];
                if ($language == 'zh') {// 中 - 俄
                    $translated = $Translation->textTranslate($name, $Source = 'zh', $Target = 'ru');
                    $name_ru = $translated['TargetText'];
                } else if ($language == 'ru') {// 俄 - 中
                    $translated = $Translation->textTranslate($name, $Source = 'ru', $Target = 'zh');
                    $name_ru = $name;
                    $name = $translated['TargetText'];
                } else {
                    // 输入其他文字时，中俄相同
                    $name_ru = $name;
                }
            }


            $id = input("id") ?? 0;
            $d['user_id'] = $user_id = input("user_id") ?? '';

            $car_category_id = input("car_category_id") ?? 0;
            if (empty($name)) {
                $name = Db::table("store_vehicle")->where(['id' => $car_category_id])->value('title');
            }
            if (empty($name_ru)) {
                $name_ru = Db::table("store_vehicle")->where(['id' => $car_category_id])->value('title_ru');
            }

            $car_number = input("car_number") ?? '';
            $find = Db::table("store_car")->where(['car_number' => $car_number])->find();
            if (empty($id)) {
                if ($find) {
                    $this->api_result(400, '车牌号重复', ['data' => []]);
                    exit;
                }
            } else {
                $find = Db::table("store_car")->where(['car_number' => $car_number])->where("user_id!=$user_id")->find();
                if ($find) {
                    $this->api_result(400, '车牌号重复', ['data' => []]);
                    exit;
                }
            }

            $d['mobile'] = Db::connect("fast_mysql")->table("fa_user")->where(['id' => $d['user_id']])->value('mobile');
            $d['name'] = $name;
            $d['name_ru'] = $name_ru;
            $d['mode'] = input("mode");
            $d['car_category_id'] = input("car_category_id");
            $d['car_number'] = input("car_number");
            $d['car_l'] = input("car_l");
            $d['car_w'] = input("car_w");
            $d['car_h'] = input("car_h");
            $d['createtime'] = time();
            $d['updatetime'] = time();
            $d['image'] = input("image") ?? '';
            $d['logo'] = input("logo");
            $d['videos'] = input("videos") ?? '';
            $d['product_weight'] = input("product_weight") ?? '0';
            $d['product_volume'] = input("product_volume") ?? '0';
            $d['goods_l'] = input("goods_l") ?? '0';
            $d['goods_w'] = input("goods_w") ?? '0';
            $d['goods_h'] = input("goods_h") ?? '0';
            $d['transport_list'] = input("transport_list") ?? '';
            $d['trailer_number'] = input("trailer_number") ?? '';
            $d['is_company'] = input("is_company") ?? '1';
            $d['driver_id'] = input("driver_id") ?? '';
            $d['current_city_id'] = input("current_city_id") ?? '';
            $d['status'] = input("status") ?? '1';
            $d['documents'] = input("documents") ?? '';

            $cfd_transports_str = '';
            $mdd_transports_str = '';
            if (!empty($d['transport_list'])) {
                $d['transport_list'] = trim($d['transport_list'], ';');
                $arr = explode(';', $d['transport_list']);
                foreach ($arr as $k => &$v) {
                    if ($v) {
                        $ar = explode(',', $v);
                        $cfd_transports_str .= $ar[0] . ',' . $ar[1] . ',' . $ar[2] . ';';
                        $mdd_transports_str .= $ar[3] . ',' . $ar[4] . ',' . $ar[5] . ';';
                    }
                }
                $d['cfd_transports'] = $cfd_transports_str;
                $d['mdd_transports'] = $mdd_transports_str;
            }

            $insertGetId = 0;
            if (!empty($id)) {
                $insertGetId = $id;
                $w['id'] = $id;
                if ($d['user_id']) {
                    if ($d['mode'] == 2) {

                        $find = Db::table("store_car")->where(['id' => $id])->find();
                        if ($find['driver_id'] == $d['driver_id']) {

                            Db::table("store_car")->where($w)->update($d);
                        } else {

                            $ishave = Db::table("store_car")->where(['mode' => 2, 'driver_id' => $d['driver_id']])->find();
                            if ($ishave) {

                                $this->api_result(400, '司机重复绑定', ['data' => []]);
                                exit;
                            } else {

                                Db::table("store_car")->where($w)->update($d);
                            }
                        }
                    } else {

                        Db::table("store_car")->where($w)->update($d);
                    }
                }
            } else {
                if ($d['mode'] == 2) {
                    if ($d['user_id']) {
                        $ishave = Db::table("store_car")->where(['mode' => 2, 'driver_id' => $d['driver_id']])->find();
                        if ($ishave) {

                            $this->api_result(400, '司机重复绑定', ['data' => []]);
                            exit;
                        } else {

                            $insertGetId = Db::table("store_car")->insertGetId($d);
                        }
                    }
                } else {

                    $insertGetId = Db::table("store_car")->insertGetId($d);
                }
            }
            $this->api_result(200, '请求成功', ['data' => $insertGetId]);
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败', ['data' => []]);
        }

    }


    /**
     * @throws \think\db\exception\DbException
     * @notes 车辆删除
     * @author: dongdong
     * @date: 2025/1/20 9:58
     */
    public function transport_del()
    {
        try {
            $id = input("id") ?? 0;
            $w['id'] = $id;
            Db::table("store_car")->where($w)->update(['is_deleted' => 1]);
            $this->api_result(200, '请求成功', ['data' => []]);
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败', ['data' => []]);
        }
    }

    /**
     * @param $name
     * @return array
     * @notes 翻译方法
     * @author: dongdong
     * @date: 2025/1/20 15:50
     */
    public function fanyi($name)
    {
        $Translation = new Translation();

        $wenzi = $Translation->languageDetect($name);
        if ($wenzi) {
            $language = $wenzi['Lang'];
            if ($language == 'zh') {// 中 - 俄
                $translated = $Translation->textTranslate($name, $Source = 'zh', $Target = 'ru');
                $name_ru = $translated['TargetText'];
            } else if ($language == 'ru') {// 俄 - 中
                $translated = $Translation->textTranslate($name, $Source = 'ru', $Target = 'zh');
                $name_ru = $name;
                $name = $translated['TargetText'];
            } else {
                // 输入其他文字时，中俄相同
                $name_ru = $name;
            }

            return ['zh' => $name, 'ru' => $name_ru];
        } else {
            return ['zh' => $name, 'ru' => $name];
        }

    }


    /**
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 司机工作台
     * @author: dongdong
     * @date: 2025/1/23 11:56
     */
    public function driver_work()
    {
        $register_id = input('register_id') ?? '';
        $data = Db::table("store_driver")->where(['register_id' => $register_id])->find();

        if ($data) {
            $data['mine'] = Db::connect("fast_mysql")->table("fa_user")->where(['id' => $register_id])->field("id,username,username_ru,mobile,level,avatar")->find();
            $data['bind_user'] = Db::connect("fast_mysql")->table("fa_user")->where(['id' => $data['bind_user_id']])->field("id,username,username_ru,mobile,level,avatar")->find();
            $data['car'] = Db::table("store_car")->where(['driver_id' => $data['id']])->select();
        } else {
            $data['mine'] = Db::connect("fast_mysql")->table("fa_user")->where(['id' => $register_id])->field("id,username,username_ru,mobile,level,avatar,salt,password,user_password")->find();

            if ($data['mine']) {
                $d['title'] = $data['mine']['username'];
                $d['title_ru'] = $data['mine']['username_ru'];
                $d['create_at'] = date('Y-m-d H:i:s');
                $d['phone'] = $data['mine']['mobile'];
                $d['type'] = 32;
                $d['password'] = $data['mine']['user_password'] ?? '';
                $d['user_password'] = $data['mine']['password'];
                $d['salt'] = $data['mine']['salt'];
                $d['state'] = 2;
                $d['register_id'] = $data['mine']['id'];

                Db::table("store_driver")->insert($d);
                $data = Db::table("store_driver")->where(['register_id' => $register_id])->find();

                $data['mine'] = Db::connect("fast_mysql")->table("fa_user")->where(['id' => $register_id])->field("id,username,username_ru,mobile,level,avatar,salt,password,user_password")->find();
                $data['bind_user'] = Db::connect("fast_mysql")->table("fa_user")->where(['id' => $data['bind_user_id']])->field("id,username,username_ru,mobile,level,avatar")->find();
                $data['car'] = Db::table("store_car")->where(['driver_id' => $data['id']])->select();
            }
        }
        $this->api_result(200, '请求成功', ['data' => $data]);
    }


    /**
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 解绑
     * @author: dongdong
     * @date: 2025/1/23 11:56
     */
    public function cancel_bind()
    {
        $id = input('id') ?? '';
        $user_id = input('user_id') ?? '';
        $remark = input('remark') ?? '';

        $find = Db::table("store_driver")->where(['id' => $id])->find();
        if ($find) {
            $insertGetId = Db::table("store_request_list")->insertGetId(['user_id' => $find['register_id'], 'service_user_id' => $user_id, 'create_at' => date('Y-m-d H:i:s'), 'type' => 0]);

            $RemarkModel = new RemarkModel();
            $content_id = 0;
            if (!empty($remark)) {
                $content_id = $RemarkModel::remark_id($remark, "store_request_list", $content_id);
            }
            Db::table("store_request_list")->where(['id' => $insertGetId])->update(['content_id' => $content_id]);
        }
        $this->api_result(200, '请求成功', ['data' => []]);
    }

    /**
     * @throws DbException
     * @notes 司机同意 公司绑定我
     * @author: dongdong
     * @date: 2025/1/23 13:39
     */
    public function bind()
    {
        try {
            $id = input('id') ?? '';
            $user_id = input('user_id') ?? '';
            $remark = input('remark') ?? '';
            $image = input('image') ?? '';

            $keyword = input('keyword') ?? '';
            if ($keyword) {
                if ($user_id == '') {
                    $fa_user = Db::connect("fast_mysql")->table("fa_user")
                        ->field("username,username_ru,mobile,avatar,id")->where("username='$keyword'")->find();
                    if ($fa_user) {
                        $user_id = $fa_user['user_id'];
                    }
                    $fa_user_ru = Db::connect("fast_mysql")->table("fa_user")
                        ->field("username,username_ru,mobile,avatar,id")->where("username_ru='$keyword'")->find();
                    if ($fa_user_ru) {
                        $user_id = $fa_user_ru['user_id'];
                    }
                }
            }

            if (empty($user_id)) {
                $this->api_result(400, '绑定用户未找到', ['data' => []]);
                exit;
            }

            $find = Db::table("store_driver")->where(['id' => $id])->find();
            if ($find) {
                $insertGetId = Db::table("store_request_list")->insertGetId(['user_id' => $find['register_id'], 'image' => $image, 'service_user_id' => $user_id, 'create_at' => date('Y-m-d H:i:s'), 'type' => 1]);

                $RemarkModel = new RemarkModel();
                $content_id = 0;
                if (!empty($remark)) {
                    $content_id = $RemarkModel::remark_id($remark, "store_request_list", $content_id);
                }
                Db::table("store_request_list")->where(['id' => $insertGetId])->update(['content_id' => $content_id]);

            }
            $this->api_result(200, '请求成功', ['data' => []]);
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败', ['data' => []]);
        }

    }


    /**
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 平台申诉
     * @author: dongdong
     * @date: 2025/1/23 11:56
     */
    public function appeal_bind()
    {
        $id = input('id') ?? '';
        $user_id = input('user_id') ?? '';
        $remark = input('remark') ?? '';

        $find = Db::table("store_driver")->where(['id' => $id])->find();
        $insertGetId = 0;
        if ($find) {
            $insertGetId = Db::table("store_request_list")->insertGetId(['user_id' => $find['register_id'], 'service_user_id' => $user_id, 'create_at' => date('Y-m-d H:i:s'), 'type' => 2]);

            $RemarkModel = new RemarkModel();
            $content_id = 0;
            if (!empty($remark)) {
                $content_id = $RemarkModel::remark_id($remark, "store_request_list", $content_id);
            }
            Db::table("store_request_list")->where(['id' => $insertGetId])->update(['content_id' => $content_id]);

        }
        $this->api_result(200, '请求成功', ['data' => $insertGetId]);
    }

    /**
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 公司提交 给司机绑定申请
     * @author: dongdong
     * @date: 2025/1/23 11:56
     */
    public function submit_bind()
    {
        $service_user_id = input('service_user_id') ?? '';
        $user_id = input('user_id') ?? '';
        $remark = input('remark') ?? '';
        $car_id = input('car_id') ?? '';

        $driver_id = Db::table("store_car")->where(['id' => $car_id])->value('driver_id');
        $bind_user_id = Db::table("store_driver")->where(['id' => $driver_id])->value('bind_user_id');
        if ($bind_user_id == $user_id) {
            $this->api_result(400, '请不要重复绑定', ['data' => []]);
        } else {
            $insertGetId = Db::table("store_request_list")->insertGetId(['user_id' => $user_id, 'car_id' => $car_id, 'service_user_id' => $service_user_id, 'create_at' => date('Y-m-d H:i:s'), 'type' => 1]);

            $RemarkModel = new RemarkModel();
            $content_id = 0;
            if (!empty($remark)) {
                $content_id = $RemarkModel::remark_id($remark, "store_request_list", $content_id);
            }
            Db::table("store_request_list")->where(['id' => $insertGetId])->update(['content_id' => $content_id]);


            $d['user_id'] = $user_id;
            $d['car_id'] = $car_id;
            $d['type'] = 2;
            $d['create_at'] = date('Y-m-d H:i:s');
            Db::table("store_history_list")->insert($d);

            $this->api_result(200, '请求成功', ['data' => $insertGetId]);
        }


    }


    /**
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 邀请绑定数据列表
     * @author: dongdong
     * @date: 2025/1/23 15:14
     */
    public function invite_bind()
    {
        $register_id = input('register_id') ?? '';

        $current = input('current') ?? '0';   //0解绑  1邀请
        $currentCheck = input('currentCheck') ?? '0'; //0已处理  1未处理

        $where = "is_deleted is null";
        if (isset($current)) {
            $where .= " and type='$current'";
        }

        if (isset($currentCheck)) {
            if ($currentCheck == '0') {
                $where .= " and status !='1'";
            } else {
                $where .= " and status ='1'";
            }
        }

        $data = Db::table("store_request_list")->where(['service_user_id' => $register_id])->where($where)->order("id desc")->select();
        $arr = [];
        $count = 0;
        if ($data) {
            foreach ($data as $k => &$v) {
                $v['mine'] = Db::connect("fast_mysql")->table("fa_user")->where(['id' => $v['service_user_id']])->field("id,username,username_ru,mobile,level,avatar")->find();
                $v['request_user'] = Db::connect("fast_mysql")->table("fa_user")->where(['id' => $v['user_id']])->field("id,username,username_ru,mobile,level,avatar")->find();
                $v['request_user']['car'] = Db::table("store_car")->where(['user_id' => $v['request_user']['id']])->find();
                $v['remark'] = Db::table("store_remark")->where(["id" => $v['content_id']])->find();

                array_push($arr, $v);
            }
            $count = count($data);
        }
        $this->api_result(200, '请求成功', ['data' => $arr, 'count' => $count]);
    }


    /**
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 邀请详情
     * @author: dongdong
     * @date: 2025/2/17 17:55
     */
    public function invite_info()
    {
        $id = input("id") ?? '0';
        $data = Db::table("store_request_list")->where(['id' => $id])->order("id desc")->select();
        $arr = [];
        foreach ($data as $k => &$v) {
            $v['mine'] = Db::connect("fast_mysql")->table("fa_user")->where(['id' => $v['service_user_id']])->field("id,username,username_ru,mobile,level,avatar")->find();
            $v['request_user'] = Db::connect("fast_mysql")->table("fa_user")->where(['id' => $v['user_id']])->field("id,username,username_ru,mobile,level,avatar")->find();
            $v['request_user']['car'] = Db::table("store_car")->where(['user_id' => $v['request_user']['id']])->find();
            $v['remark'] = Db::table("store_remark")->where(["id" => $v['content_id']])->find();
            $v['images'] = explode(',', $v['image']);
            array_push($arr, $v);
        }

        if ($arr) {
            $this->api_result(200, '请求成功', ['data' => $arr[0], 'count' => 1]);
        } else {
            $this->api_result(200, '请求成功', ['data' => [], 'count' => 0]);
        }
    }


    /**
     * @throws DbException
     * @notes 删除邀请记录
     * @author: dongdong
     * @date: 2025/1/23 16:04
     */
    public function invite_del()
    {
        $id = input('id') ?? '';
        Db::table("store_request_list")->where(['id' => $id])->update(['is_deleted' => date('Y-m-d H:i:s')]);
        $this->api_result(200, '请求成功', []);
    }

    /**
     * @throws DbException
     * @notes 修改邀请状态
     * @author: dongdong
     * @date: 2025/1/23 16:50
     */
    public function invite_status()
    {
        $id = input('id') ?? '';
        $status = input('status') ?? '';
        Db::table("store_request_list")->where(['id' => $id])->update(['status' => $status]);

        $find = Db::table("store_request_list")->where(['id' => $id])->find();
        if ($status == 2) {
            Db::table("store_driver")->where(['register_id' => $find['service_user_id']])->update(['bind_user_id' => $find['user_id']]);
        }


        $this->api_result(200, '请求成功', []);
    }

    /**
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 绑定 申请记录
     * @author: dongdong
     * @date: 2025/2/11 17:49
     */
    public function apply_bind()
    {
        $user_id = input('user_id') ?? '';
        $find = Db::table("store_request_list")->where("user_id=$user_id and type=1 and is_deleted is null")->order("id desc")->find();
        if ($find) {
            $find['bind'] = Db::connect("fast_mysql")->table("fa_user")->field("id,username,username_ru")->where(["id" => $find['service_user_id']])->find();
            $find['remark'] = Db::table("store_remark")->where(["id" => $find['content_id']])->find();

            $find['bind_user_id'] = Db::table("store_driver")->where(['register_id' => $user_id])->value('bind_user_id');
        }
        $this->api_result(200, '请求成功', ['data' => $find]);
    }

    /**
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 解绑 申请记录
     * @author: dongdong
     * @date: 2025/2/11 17:49
     */
    public function jiebang_bind()
    {
        $user_id = input('user_id') ?? '';
        $find = Db::table("store_request_list")->where("user_id=$user_id and type=0 and is_deleted is null")->order("id desc")->find();
        if ($find) {
            $find['bind'] = Db::connect("fast_mysql")->table("fa_user")->field("id,username,username_ru")->where(["id" => $find['service_user_id']])->find();
            $find['remark'] = Db::table("store_remark")->where(["id" => $find['content_id']])->find();
        }
        $this->api_result(200, '请求成功', ['data' => $find]);
    }

    /**
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 申诉 申请记录
     * @author: dongdong
     * @date: 2025/2/11 17:49
     */
    public function shensu_bind()
    {
        $user_id = input('user_id') ?? '';
        $find = Db::table("store_request_list")->where("user_id=$user_id and type=2 and is_deleted is null")->order("id desc")->find();
        if ($find) {
            $find['bind'] = Db::connect("fast_mysql")->table("fa_user")->field("id,username,username_ru")->where(["id" => $find['service_user_id']])->find();
            $find['remark'] = Db::table("store_remark")->where(["id" => $find['content_id']])->find();
        }
        $this->api_result(200, '请求成功', ['data' => $find]);
    }


    /**
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 公司同意 司机绑定
     * @author: dongdong
     * @date: 2025/2/18 13:44
     */
    public function agree_request()
    {
        $id = input('id') ?? '';
        Db::table("store_request_list")->where("id=$id")->update(['status' => 2, 'handle_at' => date('Y-m-d H:i:s')]);
        $find = Db::table("store_request_list")->where("id=$id")->find();

        $driver = Db::table("store_driver")->where(['register_id' => $find['user_id']])->find();
        if ($find['type'] == 1) {
            Db::table("store_driver")->where(['register_id' => $find['user_id']])->update(['bind_user_id' => $find['service_user_id']]);
        } else {
            Db::table("store_driver")->where(['register_id' => $find['user_id']])->update(['bind_user_id' => '']);
            Db::table("store_car")->where(['user_id' => $find['service_user_id'], 'driver_id' => $driver['id']])->update(['driver_id' => 0]);
        }
        $this->api_result(200, '请求成功', ['data' => []]);
    }

    /**
     * @throws DbException
     * @notes 公司拒绝 司机绑定
     * @author: dongdong
     * @date: 2025/2/18 13:45
     */
    public function jujue_request()
    {
        $id = input('id') ?? '';
        Db::table("store_request_list")->where("id=$id")->update(['status' => 3, 'handle_at' => date('Y-m-d H:i:s')]);
        $this->api_result(200, '请求成功', ['data' => []]);
    }


    /**
     * @throws \think\db\exception\DbException
     * @notes 添加保存仓库
     * @author: dongdong
     * @date: 2025/1/20 15:52
     */
    public function warehouse_save()
    {
        $Translation = new Translation();

        $name = input("name") ?? '';
        $flag = input("flag") ?? '0';

        $wenzi = $Translation->languageDetect($name);
        $language = $wenzi['Lang'] ?? 'zh';
        if ($language == 'zh') {// 中 - 俄
            $translated = $Translation->textTranslate($name, $Source = 'zh', $Target = 'ru');
            $name_ru = $translated['TargetText'];
        } else if ($language == 'ru') {// 俄 - 中
            $translated = $Translation->textTranslate($name, $Source = 'ru', $Target = 'zh');
            $name_ru = $name;
            $name = $translated['TargetText'];
        } else {
            // 输入其他文字时，中俄相同
            $name_ru = $name;
        }

        $brand = input("brand") ?? '';
        $brand_arr = $this->fanyi($brand);
        $brand_index = input("brand_index") ?? '0';

        $address = input("address") ?? '';
        $remark = input("remark") ?? '';

        $address_arr = $this->fanyi($address);
        $remark_arr = $this->fanyi($remark);

        $id = input("id") ?? 0;

        $d['user_id'] = input("user_id") ?? '';

        $d['name'] = $name;
        $d['name_ru'] = $name_ru;
        $d['brand_index'] = $brand_index;
        $d['brand'] = $brand_arr['zh'];
        $d['brand_ru'] = $brand_arr['ru'];
        $d['address'] = $address_arr['zh'];
        $d['address_ru'] = $address_arr['ru'];
        $d['remark'] = $remark_arr['zh'];
        $d['remark_ru'] = $remark_arr['ru'];
        $d['create_at'] = date('Y-m-d H:i:s');
        $d['status'] = input("status") ?? '0';
        $d['city_id'] = input("city_id") ?? '';
        $d['square'] = input("square") ?? '';
        $d['image'] = input("image") ?? '';

        if (!empty($id)) {
            $w['id'] = $id;
            if ($flag == 1) {
                Db::table("store_warehouse")->where($w)->update(['status' => $d['status']]);
            } else {
                Db::table("store_warehouse")->where($w)->update($d);
            }

        } else {
            $d['sn'] = date('His') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
            Db::table("store_warehouse")->insert($d);
        }

        $this->api_result(200, '请求成功', ['data' => []]);
    }


    /**
     * @throws \think\db\exception\DbException
     * @notes 仓库删除
     * @author: dongdong
     * @date: 2025/1/20 15:53
     */
    public function warehouse_del()
    {
        $id = input("id") ?? 0;
        $w['id'] = $id;
        Db::table("store_warehouse")->where($w)->update(['is_deleted' => 1]);
        $this->api_result(200, '请求成功', ['data' => []]);
    }


    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 货代仓库
     * @author: dongdong
     * @date: 2025/1/20 15:54
     */
    public function warehouse_index()
    {
        $limit = input('limit') ?? 10;
        $keyword = input('keyword') ?? '';
        $brand_index = input('brand_index') ?? 0;
        $user_id = input('user_id') ?? '';

        $w = 'is_deleted = 0';
        if (!empty($keyword)) {
            $w .= " and (name like '%$keyword%' or name_ru like '%$keyword%' or sn like '%$keyword%' )";
        }
        if (!empty($brand_index)) {
            $w .= " and brand_index = '$brand_index'";
        }

        $city_id = input('city_id') ?? '';
        if ($city_id) {
            $w .= " and city_id = '$city_id'";
        }


        $where = [];
        if ($user_id) {
            $where['user_id'] = $user_id;
        }

        $page = input('page') ?? 1;
        $page = ($page - 1) * 10;

        $list = Db::table("store_warehouse")->where($where)->where($w)->order("id desc")->limit($page, $limit)->select();

        $data = [];
        foreach ($list as $k => &$v) {
            $v['area'] = $this->city($v['city_id']);
            $v['storage_count'] = Db::table("store_storage")->where(["w_id" => $v['id']])->count();
            array_push($data, $v);
        }
        $this->api_result(200, '请求成功', ['data' => $data]);
    }

    /**
     * @throws \think\db\exception\DbException
     * @notes 货位  添加编辑
     * @author: dongdong
     * @date: 2025/1/20 15:57
     */
    public function storage_save()
    {
        $name = input("name");
        $name_arr = $this->fanyi($name);

        $id = input("id") ?? 0;


        $d['user_id'] = input("user_id") ?? '';

        $d['w_id'] = input("w_id") ?? '0';
        $d['name'] = $name_arr['zh'];
        $d['name_ru'] = $name_arr['ru'];
        $d['create_at'] = date('Y-m-d H:i:s');

        if (!empty($id)) {
            $w['id'] = $id;
            Db::table("store_storage")->where($w)->update($d);
        } else {
            Db::table("store_storage")->insert($d);
        }

        $this->api_result(200, '请求成功', ['data' => $d]);
    }


    /**
     * @throws \think\db\exception\DbException
     * @notes 货位删除
     * @author: dongdong
     * @date: 2025/1/20 15:58
     */
    public function storage_del()
    {
        $id = input("id") ?? 0;
        $w['id'] = $id;
        Db::table("store_storage")->where($w)->update(['is_deleted' => 1]);
        $this->api_result(200, '请求成功', ['data' => $id]);
    }


    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 货位管理
     * @author: dongdong
     * @date: 2025/1/20 15:58
     */
    public function storage_lists()
    {
        $limit = input('limit') ?? 100;
        $keyword = input('keyword') ?? '';

        $where = ["is_deleted" => 0];
        if (!empty($keyword)) {
            $where['name|name_ru'] = ['like', "%$keyword%"];
        }


        $where['user_id'] = input('user_id') ?? '';

        $w_id = input('w_id') ?? '';
        if (!empty($w_id)) {
            $where['w_id'] = $w_id;
        }

        $page = input('page') ?? 1;
        $page = ($page - 1) * 5;

        $list = Db::table("store_storage")->where($where)->order("id desc")->limit($page, $limit)->select();
        foreach ($list as $k => &$v) {
            $v['warehouse'] = $this->warehouse($v['w_id']);
        }
        $this->api_result(200, '请求成功', ['data' => $list]);
    }

    /**
     * @param $id
     * @return array|mixed|Db|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 仓库回显
     * @author: dongdong
     * @date: 2025/1/20 15:57
     */
    public function warehouse($id)
    {
        return Db::table("store_warehouse")->field("id,name,name_ru")->where("id=$id")->find();
    }


    /**
     * @notes 入库 出库 添加
     * @author: dongdong
     * @date: 2025/1/21 10:08
     */
    public function stock_save()
    {
        Db::startTrans();
        try {
            $name = input("name");
            $name_arr = $this->fanyi($name);
            //客户名称

            $goods = input("goods");
            $goods_arr = $this->fanyi($goods);
            //货物名称

            $d['user_id'] = input("user_id") ?? '';

            $d['goods'] = $goods_arr['zh'];
            $d['goods_ru'] = $goods_arr['ru'];

            $d['number'] = $number = input("number") ?? '0';
            $d['free_number'] = $d['number'] ?? '0';
            $d['weight'] = $weight = input("weight") ?? '';
            $d['volume'] = $volume = input("volume") ?? '';

            $d['goods_l'] = input("goods_l") ?? '';
            $d['goods_w'] = input("goods_w") ?? '';
            $d['goods_h'] = input("goods_h") ?? '';

            $d['images'] = input("images") ?? '';

            $d['w_id'] = input("w_id") ?? '0';
            $d['storage_id'] = input("storage_id") ?? '0';

            $d['create_at'] = date('Y-m-d H:i:s');
            $d['order_no'] = input("order_no") ?? '';

            $d['name'] = $name_arr['zh'];
            $d['name_ru'] = $name_arr['ru'];

            $d['city'] = input("city") ?? '';
            $d['demand'] = input("demand") ?? '';
            $d['price'] = input("price") ?? '';
            $d['price_unit'] = input("price_unit");
            $d['market_price'] = input("market_price") ?? '';
            $d['market_price_unit'] = input("market_price_unit");
            $d['type'] = input("type") ?? '0';
            $d['p_id'] = input("p_id") ?? '0';

            $stock_storage = input("stock_storage") ?? '';
            if ($stock_storage) {
                $stock_storage_arr = explode(',', $stock_storage);
                $d['w_id'] = $w_id = $stock_storage_arr[0] ?? '0';
                $d['storage_id'] = $storage_id = $stock_storage_arr[1] ?? '0';

                $weight = $weight * $number;
                $volume = $volume * $number;
                Db::query("update store_storage set number=number+$number,weight=weight+$weight,volume=volume+$volume where id=$storage_id");
                Db::query("update store_warehouse set number=number+$number,weight=weight+$weight,volume=volume+$volume where id=$w_id");
            }

            $insertGetId = Db::table("store_stock")->insertGetId($d);
            Db::commit();
            $this->api_result(200, '请求成功', ['data' => $insertGetId]);
        } catch (\Exception $e) {
            Db::rollback();
            _d($e);
            $this->api_result(400, '请求失败', ['data' => 0]);
        }
    }

    /**
     * @return array|\think\Collection|Db[]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 货物列表
     * @author: dongdong
     * @date: 2025/1/21 10:56
     */
    public function goods()
    {
        $w_id = input('w_id') ?? '';
        $storage_id = input('storage_id') ?? '';
        $where = [];
        if ($w_id) {
            $where['w_id'] = $w_id;
        }
        if ($storage_id) {
            $where['storage_id'] = $storage_id;
        }
        $data = Db::table("store_stock")->where($where)->select();
        $this->api_result(200, '请求成功', ['data' => $data, 'count' => count($data)]);
    }


    /**
     * @notes 仓库与货位
     * @author: dongdong
     * @date: 2025/1/24 16:09
     */
    public function stock_storage()
    {
        try {
            $params = $this->request->post();
            $user_id = $params['user_id'] ?? '';
            $list = Db::table("store_warehouse")->field("id,name,name_ru")->where("is_deleted=0")->where(['user_id' => $user_id])->select();
            $arr = [];
            foreach ($list as $k => &$v) {
                $v['storage'] = Db::table("store_storage")->where(['w_id' => $v['id']])->where(['user_id' => $user_id])->select();
                array_push($arr, $v);
            }

            $data = [];
            foreach ($arr as $k => &$v) {
                if ($v['storage']) {
                    foreach ($v['storage'] as $kk => &$vv) {
                        array_push($data, ['val' => $v['id'] . ',' . $vv['id'], 'name' => $v['name'] . ',' . $vv['name'], 'name_ru' => $v['name_ru'] . ',' . $vv['name_ru']]);
                    }
                }
            }

            $this->api_result(200, '请求成功', ['data' => $data]);
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败', ['data' => []]);
        }
    }

    /**
     * @notes 仓库与货位
     * @author: dongdong
     * @date: 2025/1/24 16:09
     */
    public function storage()
    {
        try {
            $params = $this->request->post();
            $user_id = $params['user_id'] ?? '';
            $w_id = $params['w_id'] ?? '';
            $data = Db::table("store_storage")->where("is_deleted=0")->where(['user_id' => $user_id, "w_id" => $w_id])->select();
            $this->api_result(200, '请求成功', ['data' => $data]);
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败', ['data' => []]);
        }
    }


    /**
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 历史记录
     * @author: dongdong
     * @date: 2025/1/24 19:35
     */
    public function stock_lists()
    {
        $limit = input('limit') ?? 5;
        $keyword = input('keyword') ?? '';
        $type = input('type') ?? '0';
        $w_id = input('w_id') ?? '';

        $where = [];
        $wh = "";
        if (!empty($keyword)) {
            //$wh = "name like '%$keyword%' or name_ru like '%$keyword%' or goods like '%$keyword%' or goods_ru like '%$keyword%' ";
            $wh = "goods like '%$keyword%' or goods_ru like '%$keyword%' ";
        }
        if (!empty($p_id)) {
            $where['p_id'] = $p_id;
        }

        if ($w_id) {
            $where['w_id'] = $w_id;
        }


        $where['user_id'] = input('user_id') ?? '';

        $page = input('page') ?? 1;
        $page = ($page - 1) * $limit;

        $list = Db::table("store_stock")->where($where)->where("type=$type")->where($wh)->order("id desc")->limit($page, $limit)->select();
        $data = [];
        foreach ($list as $k => &$v) {
            $v['warehouse'] = $this->warehouse($v['w_id']);
            $v['storage'] = Db::table("store_storage")->field("name,name_ru")->where(["id" => $v['storage_id']])->find();
            $v['port'] = $this->city($v['city']);
            $v['images_arr'] = explode(',', $v['images']);
            array_push($data, $v);
        }
        $this->api_result(200, '请求成功', ['data' => $data]);
    }

    /**
     * @notes 查询库存
     * @author: dongdong
     * @date: 2025/1/25 14:28
     */
    public function stock_find()
    {
        $id = input('id');
        try {
            $v = Db::table("store_stock")->where("id=$id")->find();
            if ($v) {
                $v['warehouse'] = $this->warehouse($v['w_id']);
                $v['storage'] = Db::table("store_storage")->field("name,name_ru")->where(["id" => $v['storage_id']])->find();
                $v['port'] = $this->city($v['city']);
                $v['images_arr'] = explode(',', $v['images']);
                $this->api_result(200, '请求成功', ['data' => $v]);
            } else {
                $this->api_result(400, '请求失败', ['data' => []]);
            }
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败', ['data' => []]);
        }
    }

    /**
     * @notes 出库
     * @author: dongdong
     * @date: 2025/1/25 15:53
     */
    public function stock_out()
    {
        $id = input('id');
        $number = (int)input('number') ?? 0;
        try {
            $find = Db::table("store_stock")->where("id=$id")->find();
            if ($find) {
                $total = Db::table("store_stock")->where("p_id=$id")->sum('number');
                $free = $find['number'] - $total;
                if ($free < $number) {
                    $this->api_result(400, '库存不足', ['data' => []]);
                } else {
                    $d['user_id'] = $find['user_id'];

                    $d['goods'] = $find['goods'];
                    $d['goods_ru'] = $find['goods_ru'];

                    $d['number'] = $number;

                    $d['weight'] = $find['weight'];
                    $d['volume'] = $find['volume'];

                    $d['goods_l'] = $find['goods_l'];
                    $d['goods_w'] = $find['goods_w'];
                    $d['goods_h'] = $find['goods_h'];

                    $d['images'] = $find['images'];

                    $d['w_id'] = $find['w_id'];
                    $d['storage_id'] = $find['storage_id'];

                    $d['create_at'] = date('Y-m-d H:i:s');
                    $d['order_no'] = $find['order_no'];

                    $d['name'] = $find['name'];
                    $d['name_ru'] = $find['name_ru'];

                    $d['city'] = $find['city'];
                    $d['demand'] = $find['demand'];
                    $d['price'] = $find['price'];
                    $d['price_unit'] = $find['price_unit'];
                    $d['market_price'] = $find['market_price'];
                    $d['market_price_unit'] = $find['market_price_unit'];
                    $d['type'] = 1;
                    $d['p_id'] = $id;

                    $insertGetId = Db::table("store_stock")->insertGetId($d);


                    $weight = $find['weight'] * $number;
                    $volume = $find['volume'] * $number;
                    $storage_id = $find['storage_id'];
                    $w_id = $find['w_id'];

                    Db::query("update store_stock set free_number=free_number-$number where id=$id");
                    Db::query("update store_storage set number=number-$number,weight=weight-$weight,volume=volume-$volume where id=$storage_id");
                    Db::query("update store_warehouse set number=number-$number,weight=weight-$weight,volume=volume-$volume where id=$w_id");

                    $this->api_result(200, '请求成功', ['data' => $insertGetId]);
                }
            } else {
                $this->api_result(400, '请求失败', ['data' => []]);
            }
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败', ['data' => []]);
        }
    }


    /**
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 评论选择
     * @author: dongdong
     * @date: 2025/2/15 16:11
     */
    public function comment_select()
    {
        $user_id = input('user_id') ?? '';
        $comment = Db::table("store_comment")->where(['service_user_id' => $user_id])->select();
        $arr = [];
        foreach ($comment as $k => &$v) {
            $v['member'] = Db::connect("fast_mysql")->table("fa_user")->where(['id' => $v['user_id']])->find();
            $v['brand'] = Db::table("store_config")->where("id", "in", $v['brand_id'])->select();
            $v['message'] = Db::table("store_remark")->where(['id' => $v['content_id']])->find();
            $img = Db::table("store_attachment")->where(['table_id' => $v['message']['table_id'], 'table_name' => 'store_comment'])->select();
            $v['message']['img'] = $img;
            array_push($arr, $v);
        }
        $this->api_result(200, '请求成功', ['data' => $arr]);
    }


    /**
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 搜素城市
     * @author: dongdong
     * @date: 2025/2/11 17:12
     */
    public function search_city()
    {
        $city_id = input('city_id') ?? '';
        $city_name = input('keyword') ?? '';

        $where = "";
        if ($city_id) {
            $where = "id=$city_id";
        } elseif ($city_name) {
            $where = "mergename like '%$city_name%' or mergename_ru like '%$city_name%'";
        }

        $select = Db::connect("fast_mysql")->table("fa_area")->field("id,name,name_ru,mergename,mergename_ru,pid,country")->where("level=2")->where($where)->select();

        if ($select) {
            $arr = [];
            foreach ($select as $k => &$v) {
                $city = Db::connect("fast_mysql")->table("fa_area")->field("id,name,name_ru,mergename,mergename_ru,pid,country")->where(["id" => $v['id']])->find();
                $province = Db::connect("fast_mysql")->table("fa_area")->field("id,name,name_ru,mergename,mergename_ru,pid,country")->where(["id" => $city['pid']])->find() ?? [];
                if (count($province)) {
                    $country = $province['country'];
                    if ($country == 0) {
                        $find = Db::connect("fast_mysql")->table("fa_location")->field("id,name,name_ru")->where(["id" => 7])->find();
                    } elseif ($country == 1) {
                        $find = Db::connect("fast_mysql")->table("fa_location")->field("id,name,name_ru")->where(["id" => 61])->find();
                    } elseif ($country == 2) {
                        $find = Db::connect("fast_mysql")->table("fa_location")->field("id,name,name_ru")->where(["id" => 37])->find();
                    } elseif ($country == 3) {
                        $find = Db::connect("fast_mysql")->table("fa_location")->field("id,name,name_ru")->where(["id" => 87])->find();
                    } else {
                        $find = ['id' => 0, 'name' => '不限', 'name_ru' => 'Без ограничений', 'value' => '-1'];
                    }
                } else {
                    $find = ['id' => 0, 'name' => '不限', 'name_ru' => 'Без ограничений', 'value' => '-1'];
                }
                array_push($arr, ['country' => $find, 'province' => $province, 'city' => $city]);
            }
            $this->api_result(200, '请求成功', ['data' => $arr]);
        } else {
            $arr = [['country' => [], 'province' => [], 'city' => []]];
            $this->api_result(200, '请求成功', ['data' => $arr]);
        }

    }

    /**
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 用户搜索接口
     * @author: dongdong
     * @date: 2025/1/23 13:45
     */
    public function search_user()
    {
        $params = $this->request->post();
        $where = [];
        $page = $params['page'] ?? 1;
        $page = ($page - 1) * 10;
        if (!empty($params['keyword'])) {
            $keyword = $params['keyword'];
            $where['username|username_ru|mobile'] = ['like', "%$keyword%"];
        }
        $count = Db::connect("fast_mysql")->table("fa_user")
            ->field("username,username_ru,mobile,avatar,id")->where($where)->count();
        $list = Db::connect("fast_mysql")->table("fa_user")
            ->field("username,username_ru,mobile,avatar,id")->where($where)->order("id desc")->limit($page, 10)->select();
        $this->api_result(200, '请求成功', ['data' => ['list' => $list, 'count' => $count]]);
    }


    /**
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 采购商搜索接口
     * @author: dongdong
     * @date: 2025/1/23 13:45
     */
    public function search_caigou_user()
    {
        $params = $this->request->post();
        $where = [];
        $whe = "";
        $page = $params['page'] ?? 1;
        $page = ($page - 1) * 10;
        if (!empty($params['keyword'])) {
            $keyword = $params['keyword'];
            $whe = "u.username like '%$keyword%' or u.username_ru like '%$keyword%' or u.mobile like '%$keyword%'";
        }

        if (!empty($params['city_id'])) {
            $where['a.area_new_id'] = $params['city_id'];
        }


        $count = Db::connect("fast_mysql")->table("fa_user")->alias("u")
            ->join("fa_user_info i", "u.id=i.user_id")->where("i.identity_id", "in", "3,10,53,54")
            ->join('fa_user_audit c','c.user_id = u.id and c.status = 2')
            ->field("u.id")->where($where)->where($whe)->count();
        $list = Db::connect("fast_mysql")->table("fa_user")->alias("u")->join("fa_user_info i", "u.id=i.user_id")->where("i.identity_id", "in", "3,10,53,54")
            ->join('fa_user_audit c','c.user_id = u.id and c.status = 2')
            ->field("u.username,u.username_ru,u.mobile,u.avatar,u.id,u.level,i.main_product,i.identity_category_id")->where($where)->where($whe)->order("u.id desc")->limit($page, 10)->select();

        $arr = [];
        foreach ($list as $k => &$v) {
            $v['user_info'] = Db::connect("fast_mysql")->table("fa_user_info")->where(['user_id' => $v['id']])->field("country,rating,area_new_id,identity_category_id")->find();
            if ($v['user_info']) {
                $v['area'] = Db::connect("fast_mysql")->table("fa_area")->where(['id' => $v['user_info']['area_new_id']])->find();
                $v['category'] = Db::connect("fast_mysql")->table("fa_category")->field("name,name_ru")->where(['id' => $v['user_info']['identity_category_id']])->find();
            } else {
                $v['area'] = [];
                $v['category'] = [];
            }
            $v['category_name'] = '';
            $v['category_name_ru'] = '';
            if($v['identity_category_id']){
                $category = Db::connect("fast_mysql")->table("fa_category")->field("name,name_ru")->whereIN('id',explode(',',$v['identity_category_id']))->select()->toarray();
                $v['category_name'] = implode(',',array_column($category,'name'));
                $v['category_name_ru'] = implode(',',array_column($category,'name_ru'));
            }
            array_push($arr, $v);
        }


        $this->api_result(200, '请求成功', ['data' => ['list' => $arr, 'count' => $count]]);
    }


    /**
     * 我的发布内容
     */
    public function my_release_state()
    {
        $params = $this->request->post();
        $user_id = $params['user_id'];
        $where = ['user_id' => $user_id];
        $page = $params['page'] ?? 1;
        $page = ($page - 1) * 10;
        $count = Db::table("store_state")->where($where)->count();
        $list = Db::table("store_state")->where($where)->order("id desc")->limit($page, 10)->select();
        $this->api_result(200, '请求成功', ['data' => ['list' => $list, 'count' => $count]]);
    }

    public function citys($id)
    {
        if ($id) {
            return Db::connect("fast_mysql")->table("fa_area")->field("shortname,shortname_ru,name,name_ru,mergename,mergename_ru,country")->where("id", "in", $id)->find();
        } else {
            return [];
        }
    }
}